<template>
  <div class="app-container">
    <div id="map"></div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="预约时间">
        <el-date-picker
          v-model="time"
          style="width: 240px"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          type="daterange"
          @change="getTime"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" max-height="650px">
      <el-table-column label="编号" align="center" prop="num" />
      <el-table-column label="订单号" align="center" prop="name" />
      <el-table-column label="归属门店" align="center" prop="couponTypeName" />
      <el-table-column label="项目名称" align="center" prop="regionName" />
      <el-table-column label="技师名称" align="center" prop="storeName" />
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="projectName" />
      <el-table-column label="退款总金额" align="center" prop="projectName" />
      <el-table-column label="退赠送金额" align="center" prop="projectName" />
      <el-table-column label="退优惠券金额" align="center" prop="projectName" />
      <el-table-column label="支付类型" align="center" prop="projectName" />
      <el-table-column label="退单状态" align="center" prop="projectName" />
      <el-table-column label="审核状态" align="center" prop="projectName" />
      <el-table-column label="审批人" align="center" prop="projectName" />
      <el-table-column label="退款方式" align="center" prop="projectName" />
      <el-table-column label="创建时间" align="center" prop="projectName" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listcoupon } from "@/api/system/coupon/coupondetailed";

import { fetchStoreList, fetchRegionList } from "@/api/system/orderList/order";

export default {
  name: "Store",
  data() {
    return {
      map: null,
      local: null,
      mk: null,
      latitude: "",
      longitude: "",
      keyWords: "",
      result: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        userName: null,
        name: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      options: [],

      region: [],
      store: [],
      time: [],

      type: [],
      project: [],
      regionOption: [],
      searchStoreOption: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "当日",
            onClick(picker) {
              picker.$emit("pick", [new Date(), new Date()]);
            },
          },
          {
            text: "昨日",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 2);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "当月",
            onClick(picker) {
              const end = new Date();
              const start = new Date(end.getFullYear(), end.getMonth(), 1);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "上月",
            onClick(picker) {
               // const end = new Date();
               const start = new Date();
              // start.setMonth(start.getMonth() - 1); // 设置开始时间为上月同一天
              // start.setDate(end.getDate()); // 确保开始时间和结束时间在同一天
              start.setMonth(start.getMonth() - 1, 1); // 设置开始时间为上月1号
              const end = new Date(start.getFullYear(), start.getMonth() + 1, 0); // 获取上月的最后一天
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getRegionOption();
  },
  methods: {
    getTime() {
      console.log(this.time, "time");
    },
    getRegionOption() {
      fetchRegionList().then((res) => {
        this.regionOption = res.data.map((rItem) => {
          return {
            value: rItem.id,
            label: rItem.regionName,
          };
        });
      });
    },

    searchRegionChange(value) {
      if (value) {
        this.queryParams.storeId = "";
      }
      fetchStoreList({ regionId: value }).then((res) => {
        this.searchStoreOption = res.data.map((rItem) => {
          return {
            label: rItem.storeName,
            value: rItem.id,
          };
        });
      });
    },

    getList() {
      this.loading = true;
      listcoupon(this.queryParams).then((response) => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleDetails() {},
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/store/export",
        {
          ...this.queryParams,
        },
        `store_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
